﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationGenerator.Lists.BusinessLogic;

namespace GUserInterface.WinForms.Lists
{
    public partial class ListWizardConfigurationControl : UserControl
    {
        private IList<BaseWizardPage> _wizardPages;
        private ListConfiguration _configurationObject;
        private BaseWizardPage _activePage;

        public ListConfiguration ConfigurationObject
        {
            set { _configurationObject = value; }
            get 
            {
                if (_configurationObject == null)
                {
                    _configurationObject = new ListConfiguration();
                }
                return _configurationObject; 
            }
        }

        public ListWizardConfigurationControl()
        {
            InitializeComponent();
               
            _wizardPages = new List<BaseWizardPage>();
            _wizardPages.Add(new TypeConfigurationControl());
            _wizardPages.Add(new ColumnsConfigurationControl());
            _wizardPages.Add(new ListDisplayingConfigurationControl());
            _wizardPages.Add(new ListActionsControl());
        }

        public bool ValidateConfiguration()
        {
            foreach (var wizardPage in _wizardPages)
            {
                if(!wizardPage.ValidateConfiguration())
                {
                    return false;
                }
            }
            return true;
        }

        public void SaveConfiguration()
        {
            foreach (var wizardPage in _wizardPages)
            {
                wizardPage.SaveConfiguration(_configurationObject);
            }
        }

        public void ShowFirstPage()
        {
            _activePage = _wizardPages[0];
            if (!Controls.Contains(_activePage))
            {
                Controls.Add(_activePage);
            }

            _activePage.ShowPage(this.pWizardSheet, ConfigurationObject);
            _activePage.ShowVisible(true);
            ShowPrevButton(false);
        }

        private void ShowPrevButton(bool show)
        {
            prevButton.Visible = show;
        }

        private void ShowNextButton(bool show)
        {
            nextButton.Visible = show;
        }

        private void prevButton_Click(object sender, EventArgs e)
        {
            if (!_activePage.ValidateConfiguration())
            {
                return;
            }

            _activePage.SaveConfiguration(ConfigurationObject);
            _activePage.ShowVisible(false);

            _activePage = GetPrevWizardPage(_activePage);
            _activePage.ShowPage(pWizardSheet, ConfigurationObject);
            _activePage.ShowVisible(true);
            if (_activePage == _wizardPages[0])
            {
                ShowPrevButton(false);
            }

            if (_activePage != _wizardPages[_wizardPages.Count - 1])
            {
                ShowNextButton(true);
            }

        }

        private BaseWizardPage GetNextWizardPage(BaseWizardPage actualPage)
        {
            for (int counter = 0; counter < _wizardPages.Count; counter++)
            {
                if (_wizardPages[counter] == actualPage)
                {
                    return _wizardPages[counter + 1];
                }
            }

            return null;
        }

        private BaseWizardPage GetPrevWizardPage(BaseWizardPage actualPage)
        {
            for (int counter = 0; counter < _wizardPages.Count; counter++)
            {
                if (_wizardPages[counter] == actualPage)
                {
                    return _wizardPages[counter - 1];
                }
            }

            return null;
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            if (!_activePage.ValidateConfiguration())
            {
                return;
            }

            _activePage.SaveConfiguration(ConfigurationObject);
            _activePage.ShowVisible(false);

            _activePage = GetNextWizardPage(_activePage);
            _activePage.ShowPage(pWizardSheet, ConfigurationObject);
            _activePage.ShowVisible(true);

            if (_activePage != _wizardPages[0])
            {
                ShowPrevButton(true);
            }

            if (_activePage == _wizardPages[_wizardPages.Count - 1])
            {
                ShowNextButton(false);
            }
        }
    }
}
